//
//  PreferenceManager.m
//  sy_pad_l
//
//  Created by Chiu Chi Ho on 3/3/13.
//  Copyright (c) 2013 Chiu Chi Ho. All rights reserved.
//

#import "PreferenceManager.h"
#import "TimeslotManager.h"

@implementation PreferenceManager

static PreferenceManager *sharedInstance;
+ (PreferenceManager *)sharedManager
{
	@synchronized(self)
	{
		if(sharedInstance == nil)
			sharedInstance = [[PreferenceManager alloc] init];
	}
	
	return sharedInstance;
}

- (id)init
{
	if(self = [super init])
	{
	}
	return self;
}

- (BOOL)isFirstLaunch
{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	id value = [defaults objectForKey:@"First_Launch"];
	
	if(value == nil)
	{
		[defaults setBool:NO forKey:@"First_Launch"];
		[defaults synchronize];
		
		return YES;
	}
	
	return NO;
}

- (NSInteger)loadThirdDigit
{
	//Check if this is the first time to use
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	id value = [defaults objectForKey:@"Third_Digit"];
	
	if(value != nil)
	{
		NSInteger groupId = [defaults integerForKey:@"Third_Digit"];
		return groupId;
	}
	
	return -1;
}

- (NSString *)loadGroupId
{
	NSInteger thirdDigit = [self loadThirdDigit];
	if(thirdDigit == -1)	//No third digit
		return nil;
	
	return [TimeslotManager convertToGroupByDigit:thirdDigit];
}

- (void)saveThirdDigit:(NSInteger)value
{
	//Save the setting
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setInteger:value forKey:@"Third_Digit"];
	[defaults synchronize];
}

- (BOOL)loadNotificationSetting
{
	//Check if this is the first time to use
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	return [defaults boolForKey:@"Allow_Local_Notif"];
}

- (void)saveNotificationSetting:(BOOL)value
{
	//Save the setting
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setBool:value forKey:@"Allow_Local_Notif"];
	[defaults synchronize];
}
@end
